/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.application.Platform
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.layout.Pane
 *  javafx.stage.Modality
 *  javafx.stage.Stage
 *  javafx.stage.WindowEvent
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.util.ObjectUtil;
import com.gys.common.business.CashierDutyUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.exception.BusinessException;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.index.IndexController;
import com.gys.controller.salesReceipts.ChangeDutyController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.example.GetLoginStoreOutData;
import com.gys.view.salesReceipts.CashierDutyView;
import com.gys.view.salesReceipts.ChangeDutyView;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class CashierDutyController
extends BaseController
implements Initializable {
    @Autowired
    private IndexController indexController;
    @Autowired
    private ChangeDutyController changeDutyController;
    @FXML
    private Pane pane;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                CashierDutyView view = SpringUtil.getBean(CashierDutyView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.setOnCloseRequest((EventHandler)new EventHandler<WindowEvent>(){

                    public void handle(WindowEvent event) {
                        CashierDutyController.this.cancelAction();
                    }
                });
            }
        }, 1000L);
    }

    @FXML
    void sureAction() {
        if (!IndexController.stage.isFocused()) {
            IndexController.stage.requestFocus();
        }
        CacheUtil.put("isFirst", false);
        CacheUtil.put("needChange", 0);
        String userId = this.getLoginUser().getUserId();
        this.salesReceiptsMainController.getWorkUserId().setText(userId);
        this.pane.getScene().getWindow().hide();
        if (!this.checkChange()) {
            CacheUtil.put("needChange", 1);
            this.changeDutyController.init();
            Platform.runLater((Runnable)new Runnable(){

                @Override
                public void run() {
                    CommonUtil.showView(ChangeDutyView.class, Modality.APPLICATION_MODAL);
                }
            });
        }
        this.salesReceiptsMainController.getInputTextField().requestFocus();
    }

    public boolean checkChange() {
        GetLoginStoreOutData loginStoreOutData = (GetLoginStoreOutData)CacheUtil.get("store");
        if (ObjectUtil.isEmpty(loginStoreOutData)) {
            return true;
        }
        HttpJson httpJson = CashierDutyUtil.checkChange(CommonUtil.convertToMap(loginStoreOutData));
        if (!httpJson.isSuccess()) {
            throw new BusinessException(httpJson.getMsg());
        }
        String outData = httpJson.getMsg();
        CacheUtil.put("empGroupName", outData);
        return ObjectUtil.isNotEmpty(outData);
    }

    @FXML
    void cancelAction() {
        this.pane.getScene().getWindow().hide();
        IndexController.stage.close();
    }
}

